<?php


class FashionAction extends BaseAction
{
    public function index()
    {
        $data = [
            'code' => 100,
            'msg' => '获取信息失败',
            'data' => [],
        ];
        $indexData = [];
        $slide = D('w3g_slide_show')->field('`image`, `url`')->order('sort')->limit(3)->select();
        if (isset($slide)) {
            $slide_info = array();
            foreach ($slide as $key => &$value) {
                $d = D('attach')->field('save_path,save_name')->where(array('attach_id' => $value['image']))->find();
                $value_info['image'] = getImageUrl($d['save_path'] . $d['save_name']);
                $value_info['url'] = $value['url'];
                $slide_info[] = $value_info;
            }
            $indexData['slide'] = $slide_info;
        }
        $notice = D('notice')->field('notice_id,title')->order('created desc')->where(array('state' => 1))->limit(3)->select();
        if (isset($notice)) {
            $indexData['notice'] = $notice;
        }
        $eight = D('index_eight')->field('title,picture,show_link')->where(array('state' => 1, 'is_audit' => 1))->order('sort')->select();
        if (isset($eight)) {
            $nav = array();
            foreach ($eight as $key => $value) {
                $item['title'] = $value['title'];
                $d = D('attach')->field('save_path,save_name')->where(array('attach_id' => $value['picture']))->find();
                $item['picture'] = getImageUrl($d['save_path'] . $d['save_name']);
                $item['show_link'] = $value['show_link'];
                $nav[] = $item;
            }
            $indexData['nav'] = $nav;
        }
        $free_data = D('index_page')->field('picture,show_link,show_number')->where(array('state' => 1, 'is_audit' => 1))->order('sort')->select();
        if (isset($free_data)) {
            $free = array();
            foreach ($free_data as $key => $value) {
                $pictures = json_decode($value['picture']);
                $links = json_decode($value['show_link']);
                $free_info = array();
                for ($i = 0; $i < count($pictures); $i++) {
                    $item = array();
                    $d = D('attach')->field('save_path,save_name')->where(array('attach_id' => $pictures[$i]))->find();
                    if ($d) {
                        $item['picture'] = getImageUrl($d['save_path'] . $d['save_name']);
                        $item['show_link'] = $links[$i];
                    }
                    $free_info['show_number'] = $value['show_number'];
                    $free_info['show_image'][] = $item;
                }
                $free[] = $free_info;
            }
            $indexData['free'] = $free;
        }
        if (!empty($indexData)) {
            $data['code'] = 200;
            $data['data'] = $indexData;
            $data['msg'] = '获取成功';
        }
        echo json_encode($data);
        exit();
    }

    /**
     * 品牌列表
     */
    public function brand_list()
    {
        $data = [
            'code' => 100,
            'msg' => '获取失败',
            'data' => []
        ];
        $list = $this->http_request($this->api_brand_list);
        $d = json_decode($list, true);

        if ($d['status'] == 200 && isset($d['data'])) {
            $data['code'] = 200;
            $data['msg'] = '获取成功';
            $data['data'] = $d['data'];
        }

        if ($d['status'] == 200 && !isset($d['data'])) {
            $data['msg'] = '未找到品牌信息';
        }

        echo json_encode($data);
        exit();
    }

    /**
     * 设计师列表
     */
    public function designer_list()
    {
        $data = [
            'code' => 100,
            'msg' => '获取失败',
            'data' => []
        ];
        $url = $this->api_designer_list;
        $brand_id = isset($_GET['brand_id']) ? t($_GET['brand_id']) : '';
        if (!empty($brand_id)) {
            $url .= '?brandId=' . $brand_id;
        }

        $list = $this->http_request($url);
        $d = json_decode($list, true);

        if ($d['status'] == 200 && isset($d['data'])) {
            $data['code'] = 200;
            $data['msg'] = '获取成功';
            $data['data'] = $d['data'];
        }

        if ($d['status'] == 200 && !isset($d['data'])) {
            $data['msg'] = '未找到设计师信息';
        }

        echo json_encode($data);
        exit();
    }

    /**
     * 系列列表
     */
    public function series_list()
    {
        $data = [
            'code' => 100,
            'msg' => '获取失败',
            'data' => []
        ];
        $url = $this->api_series_all_list;
        $brand_id = isset($_GET['brand_id']) ? strval($_GET['brand_id']) : '';
        if (!empty($brand_id)) {
            $url = $this->api_series_brand_list . '?brandId=' . $brand_id . '&uid=';
        }
        $list = $this->http_request($url);
        $d = json_decode($list, true);
        if (isset($d['data']) && $d['status'] == 200) {
            $data['code'] = 200;
            $data['msg'] = '获取成功';
            $data['data'] = $d['data'];
        }

        if ($d['status'] == 200 && !isset($d['data'])) {
            $data['msg'] = '未找到系列信息';
        }

        echo json_encode($data);
        exit();
    }

    /**
     * 新品列表
     */
    public function product()
    {
        $data = [
            'code' => 100,
            'msg' => '获取失败',
            'data' => []
        ];
        $products = D('product')->field('product_id,title,out_link,picture,created')->where(array('state' => 1))->order('created desc')->select();
        if (!empty($products)) {
            foreach ($products as $key => &$val) {
                $picture = D('attach')->where(array('attach_id' => $val['picture']))->find();
                if (isset($picture)) {
                    $val['picture'] = getImageUrl($picture['save_path'] . $picture['save_name']);
                }
            }
            $data['code'] = 200;
            $data['msg'] = '获取成功';
            $data['data'] = $products;
        }
        echo json_encode($data);
        exit();
    }

    /**
     * 话题列表
     */
    public function topic_list()
    {
        $data = [
            'code' => 100,
            'msg' => '获取失败',
            'data' => []
        ];
        $topics = D('feed_topic')->field('topic_id,topic_name,count,ctime,is_history,is_default,is_top')->where(array('status' => 1, 'lock' => 0, 'is_history' => 0, 'is_hidden' => 0))->order('is_top desc,ctime desc,sort')->select();
        if (is_array($topics) && count($topics) > 0) {
            $data['code'] = 200;
            $data['msg'] = '获取成功';
            $data['data'] = $topics;
        }
        if (count($topics) == 0) {
            $data['code'] = 201;
            $data['msg'] = '当前没有话题';
        }

        echo json_encode($data);
        exit();
    }

    /**
     * 历史话题
     */
    public function topic_history()
    {
        $data = [
            'code' => 100,
            'msg' => '获取失败',
            'data' => []
        ];
        $topics = D('feed_topic')->field('topic_id,topic_name,count,ctime,is_history,is_default,is_top')->where(array('status' => 1, 'is_history' => 1, 'is_hidden' => 0))->order('ctime desc')->select();
        if (is_array($topics) && count($topics) > 0) {
            $data['code'] = 200;
            $data['msg'] = '获取成功';
            $data['data'] = $topics;
        }
        if (count($topics) == 0) {
            $data['code'] = 201;
            $data['msg'] = '当前没有历史话题';
        }

        echo json_encode($data);
        exit();
    }

    /**
     * 获取分享列表/话题分享列表
     */
    public function feed_list()
    {
        $data = [
            'code' => 100,
            'msg' => '获取失败',
            'data' => []
        ];
        $topic_id = isset($_GET['topic_id']) ? $_GET['topic_id'] : '';
        $limit = isset($_GET['limit']) ? $_GET['limit'] : 0;
        $feed_ids = D('feed')->order('publish_time desc')->getField('feed_id', true, 'is_del = 0 AND is_audit = 1', $limit);
        if (!empty($topic_id)) {
            unset($feed_ids);
            $feeds = D('feed')->field('ts_feed.feed_id')
                ->join("ts_feed_data AS d ON d.feed_id=ts_feed.feed_id")
                ->where(array('d.topic_id' => $topic_id, 'ts_feed.is_del' => 0, 'ts_feed.is_audit' => 1))
                ->order('ts_feed.publish_time desc')->limit($limit)->select();
            foreach ($feeds as $k => $value) {
                $feed_ids[] = $value['feed_id'];
            }
        }
        $feeds = model('feed')->formatFeed($feed_ids, true);
        if (!empty($feeds)) {
            foreach ($feeds as $key => &$val) {
                if ($val["is_hidden"] == 1) {
                    unset($feeds[$key]);
                    continue;
                }
                $where = ['type' => 1, 'app' => 'w3g', 'table' => 'feed', 'row_id' => $val['feed_id']];
                $comment = model('Comment')->getCommentList($where);
                $val['comment'] = isset($comment) ? $comment : [];
            }
            $data['code'] = 200;
            $data['msg'] = '获取成功';
            $data['data'] = $feeds;
        }
        echo json_encode($data);
        exit();
    }

    public function feedNewList()
    {
        // 获取GET与POST数据
        $_REQUEST = $_GET + $_POST;
        // 查询是否有分页
        if (!empty($_REQUEST['p'])) {
            unset($_REQUEST['loadId']);
            $this->limitnums = isset($_REQUEST['limit']) ? $_REQUEST['limit'] : 10;
        } else {
            $return = array(
                'status' => -1,
                'msg' => L('PUBLIC_LOADING_ID_ISNULL'),
            );
            $_REQUEST['loadId'] = intval($_REQUEST['loadId']);
            $this->limitnums = isset($_REQUEST['limit']) ? $_REQUEST['limit'] : 10;
        }
        if (empty($_REQUEST['loadId'])) {
            $_REQUEST['loadId'] = 0;
        }
        $content = $this->getData($_REQUEST);
        // 查看是否有更多数据
        if (!$content) {
            $return = array(
                'status' => 0,
                'msg' => '没有更多分享',
            );
        } else {
            $return = array(
                'status' => 1,
                'msg' => L('PUBLIC_SUCCESS_LOAD'),
            );
            $return['feed_data'] = $content['feed_data'];
            $return['loadId'] = $content['lastId'];
            $return['firstId'] = (empty($_REQUEST['p']) && empty($_REQUEST['loadId'])) ? $content['firstId'] : 0;
        }
        exit(json_encode($return));
    }

    public function feed()
    {
        $data = [
            'code' => 100,
            'msg' => '获取失败',
            'data' => []
        ];
        $feed_id = isset($_GET['feed_id']) ? $_GET['feed_id'] : '';
        if (!empty($feed_id)) {
            $feed = model('Feed')->getFeedInfo($feed_id, true);
            $where = ['type' => 1, 'app' => 'w3g', 'table' => 'feed', 'row_id' => $feed_id];
            $comment = model('Comment')->getCommentList($where);
            $this->doContentLog($feed_id);
            $feed['comment'] = $comment;
            $data['code'] = 200;
            $data['msg'] = '获取成功';
            $data['data'] = $feed;
        }
        echo json_encode($data);
        exit();
    }


    //后台内容分析：content_stats 表的统计数据
    public function doContentLog($feed_id)
    {
        $res = d('feed')->setInc('visitor_count', array('feed_id' => $feed_id), 1);
        model('Feed')->cleanCache($feed_id);
    }

    /**
     * 品牌详情
     */
    public function brand()
    {
        $data = [
            'code' => 100,
            'msg' => '获取失败',
            'data' => []
        ];
        $brand_id = isset($_GET['brand_id']) ? t($_GET['brand_id']) : '';
        if (!empty($brand_id)) {
            $rs_user_brand = model('UserBrand')->where(array('uid' => $this->uid, 'brand_id' => $brand_id, 'status' => 1))->find();
            $url = 'https://base.zhuritec.com:8766/api/yibum/p/brand/' . $brand_id;
            $brand = $this->http_request($url);
            $d = json_decode($brand, true);
            $has_collected = $rs_user_brand ? 1 : 0;
            if (is_array($d) && $d['status'] == 200 && isset($d['data'])) {
                $data['code'] = 200;
                $data['msg'] = '获取成功';
                $data['data'] = $d['data'];
                $data['is_collected'] = $has_collected;
            } else {
                if (isset($d['status']) && $d['status'] != 200) {
                    $data['msg'] = '未找到该品牌信息';
                }
            }
        } else {
            $data['code'] = 100;
            $data['msg'] = '参数错误';
            $data['data'] = [];

        }

        echo json_encode($data, JSON_UNESCAPED_UNICODE);
        exit();
    }


    /**
     * 设计师详情
     */
    public function designer()
    {
        $data = [
            'code' => 100,
            'msg' => '获取失败',
            'data' => []
        ];
        $designer_id = isset($_GET['designer_id']) ? t($_GET['designer_id']) : '';
        if (!empty($designer_id)) {
            $rs_user_designer = model('UserDesigner')->where(array('uid' => $this->uid, 'designer_id' => $designer_id, 'status' => 1))->find();
            $has_collected = isset($rs_user_designer) ? 1 : 0;
            $url = 'https://base.zhuritec.com:8766/api/yibum/p/designer/' . $designer_id;
            $designer = $this->http_request($url);
            $d = json_decode($designer, true);

            if (is_array($d) && $d['status'] == 200) {
                $data['code'] = 200;
                $data['msg'] = '获取成功';
                $data['data'] = $d['data'];
                $data['is_collected'] = $has_collected;
            } else {
                if (is_string($d) || $d['status'] != 200) {
                    $data['msg'] = '未找到该设计师信息';
                }
            }
        }

        echo json_encode($data, JSON_UNESCAPED_UNICODE);
        exit();
    }

    /**
     * 系列详情
     */
    public function series()
    {
        $data = [
            'code' => 100,
            'msg' => '获取失败',
            'data' => []
        ];
        $series_id = isset($_GET['series_id']) ? t($_GET['series_id']) : '';
        if (!empty($series_id)) {
            $url = $this->api_series . $series_id;
            $series = $this->http_request($url);

            $d = json_decode($series, true);
            if (is_array($d) && isset($d['status']) && $d['status'] == 200) {
                $data['code'] = 200;
                $data['msg'] = '获取成功';
                $data['data'] = $d['data'];
            } else {
                if (is_string($d) || $d['status'] != 200) {
                    $data['msg'] = '未找到该品牌信息';
                }
            }
        }
        echo json_encode($data, JSON_UNESCAPED_UNICODE);
        exit();
    }

    /**
     * 公告列表
     */
    public function notice_list()
    {
        $data = [
            'code' => 100,
            'msg' => '获取失败',
            'data' => []
        ];
        $row = D('notice')->where(array('state' => 1))->select();
        if ($row) {
            $data['code'] = 200;
            $data['msg'] = '获取成功';
            $data['data'] = $row;
        }
        echo json_encode($data);
        exit();
    }

    /**
     * 公告详情
     */
    public function notice()
    {
        $data = [
            'code' => 100,
            'msg' => '获取失败',
            'data' => []
        ];
        $notice_id = isset($_GET['notice_id']) ? $_GET['notice_id'] : '';
        if (!empty($notice_id)) {
            $row = D('notice')->field('notice_id,title,content,uid')->where(array('notice_id' => $notice_id, 'state' => 1))->find();
            if ($row) {
                $data['code'] = 200;
                $data['msg'] = '获取成功';
                $data['data'] = $row;
            }
        }

        echo json_encode($data);
        exit();
    }

    public function denounce_reason()
    {
        $data = [
            'status' => 100,
            'msg' => '获取失败',
            'data' => []
        ];
        $uid = $this->uid;
        if (!$uid) {
            $data['status'] = 102;
            $data['msg'] = '未登录!';
            echo json_encode($data);
            exit();
        }
        $reasons = D('denounce_reason')->field('reason_id,reason_name')->where(array('status' => 0))->select();
        if ($reasons) {
            $data['status'] = 200;
            $data['msg'] = '获取成功';
            $data['data'] = $reasons;
        }
        echo json_encode($data);
        exit();
    }

    private function getData(array $var)
    {
        // # 安全处理
        $var['feed_key'] = t($var['feed_key']);

        // # 不存在默认赋值1
        isset($var['cancomment']) or $var['cancomment'] = 1;

        // # 模式
        $var['cancomment_old_type'] = array('post', 'repost', 'postimage', 'postfile', 'weiba_post', 'weiba_repost', 'blog_post', 'blog_repost', 'event_post', 'event_repost', 'vote_post', 'vote_repost', 'photo_post', 'photo_repost');

        // # 合并配置
        $var = array_merge($var, model('Xdata')->get('admin_Config:feed'));

        $var['remarkHash'] = model('Follow')->getRemarkHash($this->mid);

        $map = $list = array();
        $type = $var['new'] ? 'new' . $var['type'] : $var['type']; // 最新的分享与默认分享类型一一对应

        if (!empty($var['feed_key'])) {
            // 关键字匹配 采用搜索引擎兼容函数搜索 后期可能会扩展为搜索引擎
            $list = model('Feed')->searchFeed($var['feed_key'], 'all', $var['loadId'], isset($var['limit']) ? $var['limit'] : 10);
        } else {
            $where = ' is_audit=1  AND is_del = 0  AND is_hidden = 0 ';
            if ($var['loadId'] > 0) { // 非第一次
                $where .= " AND feed_id < '" . intval($var['loadId']) . "'";
            }
            if (!empty($var['feed_type'])) {
                if ($var['feed_type'] == 'post') {
                    $where .= ' AND is_repost = 0';
                } elseif ($var['feed_type'] == 'repost') {
                    $where .= " AND type LIKE '%repost'";
                } else {
                    $where .= " AND type = '" . t($var['feed_type']) . "'";
                }
            }

            // 设定可查看的全站分享总数，可以提高大数据量下的查询效率
            $max = null; //10000;
            //$list = model('Feed')->getList($where, $this->limitnums, '', $max);（旧代码）
            $list = model('Feed')->getListNew($where, isset($var['limit']) ? $var['limit'] : 10, 'publish_time', 'DESC');
        }

//        isset($list['html']) && $var['html'] = $list['html'];
        if (!empty($list['data'])) {
            $feed_ids = [];
            foreach ($list['data'] as $k) {
                $feed_ids[] = $k['feed_id'];
            }
            $content['feed_data'] = model('feed')->formatFeed($feed_ids, true);
            foreach ($content['feed_data'] as $key => &$val) {
                $where = ['type' => 1, 'app' => 'w3g', 'table' => 'feed', 'row_id' => $val['feed_id']];
                $comment = model('Comment')->getCommentList($where);
                $val['comment'] = isset($comment) ? $comment : [];
            }
            $content['firstId'] = $var['firstId'] = $list['data'][0]['feed_id'];
            $content['lastId'] = $var['lastId'] = $list['data'][(count($list['data']) - 1)]['feed_id'];
            return $content;
        }
        return false;
    }

}